package kony;

import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.io.File;

/**
 * Interface to native C KAprilTag library.
 */

public class KApriltagNative {
    private static int mProNum = 1;
    public static String TAG = "KONY_LOG";
    public static String mslogPath;
    public static File mfLogFile;

    static {
        //加载kapriltag
        System.loadLibrary("kapriltag");

        native_init(0.2, 1270);

        Log.i(TAG, "KApriltag version: " + get_version());
    }

    //初始化kapriltag
    //tag_size 二维码边框长度，单位米
    //focal_length 焦距
    public static native void native_init(double tag_size, double focal_length);

    //获取版本号
    public static native String get_version();

    //原始接口，不对外
    public static native ArrayList<KApriltag3D> get_kapriltag_3d2(double gpx_lng, double gps_lat, double gps_height, byte[] src, int width, int height);

    //获取图像中二维码的三维坐标信息
    //gpx_lng GPS经度
    //gps_lat GPS纬度
    //gps_height GPS高度
    //src 图像内容
    //width 图像水平像素
    //height 图像垂直像素
    //debug 是否输出debug日志
    //log_path 输出debug日志路径
    //get_kapriltag_3d 返回结构体，详见后面章节
    public static ArrayList<KApriltag3D> get_kapriltag_3d(double gpx_lng, double gps_lat, double gps_height, byte[] src, int width, int height, boolean debug, String log_path) throws IOException {
        if(!debug) {
            return get_kapriltag_3d2(gpx_lng, gps_lat, gps_height, src, width, height);
        }

        if (mProNum == 1) {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
            String dateTime=sdf.format(new java.util.Date());

            mslogPath = log_path + "/" + dateTime;

            File logPath = new File(mslogPath);
            if (!logPath.delete()) {
                Log.i(TAG, "delete false: " + logPath.getPath());
            }
            if (!logPath.mkdirs()) {
                Log.i(TAG, "mkdirs false " + logPath.getPath());
            }

            mfLogFile = new File(logPath.getPath() + "/log.txt");
            mfLogFile.delete();
            mfLogFile.createNewFile();
        }

        ArrayList<KApriltag3D> ret = get_kapriltag_3d2(gpx_lng, gps_lat, gps_height, src, width, height);

        RandomAccessFile raf = new RandomAccessFile(mfLogFile, "rwd");
        raf.seek(mfLogFile.length());
        String strContent = String.valueOf(mProNum)
            + "\t\t" + String.valueOf(width)
            + "\t\t" + String.valueOf(height);

        if(ret.size() > 0) {
            strContent += "\t\t" + String.valueOf(ret.get(0).x)
                    + "\t\t" + String.valueOf(ret.get(0).y)
                    + "\t\t" + String.valueOf(ret.get(0).distance);
        }

        strContent += "\n";

        raf.write(strContent.getBytes());
        raf.close();

        File fGrayFile = new File(mslogPath + "/" + String.valueOf(mProNum) + ".gray");
        fGrayFile.delete();
        fGrayFile.createNewFile();
        FileOutputStream  fos = new FileOutputStream(fGrayFile);
        fos.write(src, 0, src.length);
        fos.flush();
        fos.close();
        mProNum++;

        return ret;
    }
}
